﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using com.lover.astd.common.config;
using com.lover.astd.common.model;
using com.lover.astd.common;

namespace com.lover.astd.game.ui.server.impl.economy
{
    /// <summary>
    /// 集市服务
    /// </summary>
    class MarketServer : LogicServer
    {
        public MarketServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_Market; ServerReadableName = ConfigStrings.SR_Market; }
        public override void renderSettings()
        {
            MainForm frm = _mainForm;
            Dictionary<string, string> conf = getConfig(ServerName);
            frm.chk_market_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");
            frm.chk_market_notbuy_iffail.Checked = conf.ContainsKey(ConfigStrings.notbuy_iffail) && conf[ConfigStrings.notbuy_iffail].ToLower().Equals("true");
            if(conf.ContainsKey(ConfigStrings.items))
            {
                string items_conf = conf[ConfigStrings.items];
                string[] confs = items_conf.Split(',');
                // first unchecked all
                frm.chk_market_gem.Checked = frm.chk_market_gem_gold.Checked = false;
                frm.chk_market_stone.Checked = frm.chk_market_stone_gold.Checked = false;
                frm.chk_market_silver.Checked = false;  frm.chk_market_silver_gold.Checked = true;
                frm.chk_market_redweapon.Checked = frm.chk_market_redweapon_gold.Checked = false;
                frm.chk_market_purpleweapon.Checked = frm.chk_market_purpleweapon_gold.Checked = false;
                frm.chk_market_force_attack.Checked = false;

                foreach (string iconf in confs)
                {
                    if (iconf == null || iconf.Length == 0) continue;
                    string[] subs = iconf.Split(':');
                    if (subs.Length < 3) continue;
                    string subname = subs[0], qualitystr = subs[1], use_gold = subs[2];
                    int quality = -1;
                    if (!Int32.TryParse(qualitystr, out quality)) quality = 5;
                    if (subname.Equals("宝石"))
                    {
                        frm.chk_market_gem.Checked = true;
                        frm.chk_market_gem_gold.Checked = use_gold.ToLower().Equals("true");
                        if(quality <= 5) frm.combo_market_gem_type.SelectedIndex = quality - 1;
                    }
                    else if (subname.Equals("玉石"))
                    {
                        frm.chk_market_stone.Checked = true;
                        frm.chk_market_stone_gold.Checked = use_gold.ToLower().Equals("true");
                        if (quality <= 5) frm.combo_market_stone_type.SelectedIndex = quality - 1;
                    }
                    else if (subname.Equals("银币"))
                    {
                        frm.chk_market_silver.Checked = true;
                        frm.chk_market_silver_gold.Checked = true;
                        if (quality <= 5) frm.combo_market_silver_type.SelectedIndex = quality - 1;
                    }
                    else if (subname.Equals("redweapon"))
                    {
                        frm.chk_market_redweapon.Checked = true;
                        frm.chk_market_redweapon_gold.Checked = use_gold.ToLower().Equals("true");
                        if (quality <= 5) frm.combo_market_redweapon_type.SelectedIndex = quality - 1;
                    }
                    else if (subname.Equals("purpleweapon"))
                    {
                        frm.chk_market_purpleweapon.Checked = true;
                        frm.chk_market_purpleweapon_gold.Checked = use_gold.ToLower().Equals("true");
                        if (quality <= 5) frm.combo_market_purpleweapon_type.SelectedIndex = quality - 1;
                    }
                    else if (subname.Equals("强攻令"))
                    {
                        frm.chk_market_force_attack.Checked = true;
                    }
                }
            }
        }
        public override void saveSettings()
        {
            MainForm frm = _mainForm;
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_market_enable.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.notbuy_iffail, frm.chk_market_notbuy_iffail.Checked.ToString());
            string items = "";
            if (frm.chk_market_gem.Checked)
            {
                items += String.Format("宝石:{0}:{1},", frm.combo_market_gem_type.SelectedIndex + 1, frm.chk_market_gem_gold.Checked.ToString());
            }
            if (frm.chk_market_stone.Checked)
            {
                items += String.Format("玉石:{0}:{1},", frm.combo_market_stone_type.SelectedIndex + 1, frm.chk_market_stone_gold.Checked.ToString());
            }
            if (frm.chk_market_silver.Checked)
            {
                items += String.Format("银币:{0}:{1},", frm.combo_market_silver_type.SelectedIndex + 1, frm.chk_market_silver_gold.Checked.ToString());
            }
            if (frm.chk_market_redweapon.Checked)
            {
                items += String.Format("redweapon:{0}:{1},", frm.combo_market_redweapon_type.SelectedIndex + 1, frm.chk_market_redweapon_gold.Checked.ToString());
            }
            if (frm.chk_market_purpleweapon.Checked)
            {
                items += String.Format("purpleweapon:{0}:{1},", frm.combo_market_purpleweapon_type.SelectedIndex + 1, frm.chk_market_purpleweapon_gold.Checked.ToString());
            }
            if (frm.chk_market_force_attack.Checked)
            {
                items += "强攻令:1:true,";
            }
            if (items.Length > 0) items = items.Substring(0, items.Length - 1);
            conf.setConfig(ServerName, ConfigStrings.items, items);
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.notbuy_iffail, "false");
            conf.setConfig(ServerName, ConfigStrings.items, "宝石:1:false,玉石:2:false,purpleweapon:2:false,强攻令:1:true");
            renderSettings();
        }
    }
}
